# -*- coding:utf-8 -*-
from com.zzh.utils.dbUtils import MysqlUtils
from com.zzh.utils.jsonUtils_v2.table2Json_Chen import Table2Json
from com.zzh.utils.pagingUtils import MyPaging
from com.zzh.utils.sqlUtils.SqlUtils import SqlUtils
from com.zzh.utils.requestFieldMapDBField import fieldMap_Chen


class ViewDeptCategory(object):
    def __init__(self):
        self.conn = MysqlUtils.get_mysql_connections_v2()

    def queryData(self):
        """
        查询 view_city
        :return:
        """
        sql = "select * from view_dept_category"
        cur = self.conn.cursor()
        rs = cur.execute(sql)
        info = None
        if rs:
            list0 = cur.fetchall()
            info = Table2Json.view_dept_category_list_to_json(list0)
        cur.close()
        self.conn.close()
        return info

    def queryByCondition(self, **kwargs):
        """
        条件查询
        :param kwargs:
        :return:
        """
        pageIndex = kwargs['pageIndex']
        pageSize = kwargs['pageSize']
        conditions = kwargs['conditions']
        multiSort = kwargs['multiSort']

        # 获取对应的映射字典
        fieldMap = fieldMap_Chen.viewDeptCategory
        whereSentence = SqlUtils.getOption(conditions, fieldMap)

        cur = self.conn.cursor()
        if multiSort:
            # 排序拼接
            sortRule = SqlUtils.getSortRule(multiSort, fieldMap)
            sql = "SELECT * FROM view_dept_category WHERE " + whereSentence + " ORDER BY " + sortRule
        else:
            sql = "SELECT * FROM view_dept_category WHERE " + whereSentence
        rs = cur.execute(sql)

        if rs:
            adinfo = cur.fetchall()
            lists = MyPaging().getPage(rs, pageIndex, pageSize, adinfo)
            adJson = Table2Json.view_dept_category_list_to_json(lists)
            return adJson, rs
        else:
            return None, 0



